package com.zhangtai.modules.service.impl.employee;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.EmployeeChangeDao;
import com.zhangtai.modules.entity.employee.EmployeeChangeEntity;
import com.zhangtai.modules.service.employee.EmployeeChangeService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class EmployeeChangeServiceImpl extends ServiceImpl<EmployeeChangeDao, EmployeeChangeEntity> implements EmployeeChangeService {

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<EmployeeChangeEntity> page = this.page(
                new Page<EmployeeChangeEntity>(pageNum,pageSize),
                new QueryWrapper<EmployeeChangeEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<EmployeeChangeEntity> getList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "unitIds", required = false) List<Long> unitIds,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ){
        QueryWrapper<EmployeeChangeEntity> queryWrapper = new QueryWrapper<EmployeeChangeEntity>().
                like(!StringUtils.isEmpty(name),"name",name).eq("is_delete",0)
                .in(!CollectionUtils.isEmpty(unitIds),"unit_id",unitIds)
                .eq(CollectionUtils.isEmpty(unitIds),"unit_id",null)
                .eq(!ObjectUtils.isEmpty(status),"status",status)
                .eq(!ObjectUtils.isEmpty(type),"type",type)
                .orderByDesc("create_time");
//        employeeWrapper.and(wra->wra.in(!CollectionUtils.isEmpty(unitIds),"unit_id",unitIds).or().
//                eq("principal_user_code",code).or().eq(CollectionUtils.isEmpty(unitIds),"unit_id",null)) .orderByDesc("update_time");
        IPage<EmployeeChangeEntity> page = this.page(
                new Page<EmployeeChangeEntity>(pageNum,pageSize),
                queryWrapper
        );

        return new PageResult(page);
    }

    @Override
    public EmployeeChangeEntity getByIdE(Long id) {
            EmployeeChangeEntity one = this.getOne(new QueryWrapper<EmployeeChangeEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(EmployeeChangeEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(EmployeeChangeEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            EmployeeChangeEntity entity = new EmployeeChangeEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}